package com.example.common.utils;

import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class utilsTest {
    private static final String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    private static final String accessKeyId = "LTAI5tMxeWnxgogzqgjQCGr8";
    private static final String accessKeySecret = "JsC4GLYj2AfFt2d75kc6XMNMLpRR5T";
    private static final String bucketName = "yyz-soft";
    private static final String regionId = "cn-shanghai";

    public static void main(String[] args) throws com.aliyuncs.exceptions.ClientException {
        SendCode_2("13574501560", "Test");
    }

    public static boolean SendCode_2(String phone, String code) throws com.aliyuncs.exceptions.ClientException {
        IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(phone);
        request.setSignName("阿里云短信测试");
//        request.setTemplateCode("SMS_318845764");
        request.setTemplateCode("SMS_154950000");
        request.setTemplateParam("{\"code\":\"" + code + "\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            if ("OK".equals(response.getCode())) {
                System.out.println("短信发送成功");
                return true;
            } else {
                System.out.println("短信发送失败，ErrorCode: " + response.getCode() + ", ErrorMessage: " + response.getMessage());
                // 根据不同的错误码进行更详细的处理
                if ("isv.MOBILE_NUMBER_ILLEGAL".equals(response.getCode())) {
                    System.out.println("手机号码非法，请检查输入");
                    return false;
                }
            }
        } catch (ClientException e) {
            System.out.println("短信发送失败，ErrorMsg: " + e.getMessage());
            e.printStackTrace();
        }
        return false;
    }


}
